package org.jivesoftware.smack;

import java.util.Collection;
import java.util.Iterator;
import org.jivesoftware.smack.packet.StreamError;

public class ReconnectionManager
  implements ConnectionListener
{
  private Connection connection;
  boolean done = false;

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated()
      {
        paramConnection.addConnectionListener(new ReconnectionManager(paramConnection, null));
      }
    });
  }

  private ReconnectionManager(Connection paramConnection)
  {
    this.connection = paramConnection;
  }

  private boolean isReconnectionAllowed()
  {
    int i;
    if ((!(this.done)) && (!(this.connection.isConnected())) && (this.connection.isReconnectionAllowed()))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void connectionClosed()
  {
    this.done = true;
  }

  public void connectionClosedOnError(Exception paramException)
  {
    this.done = false;
    if (paramException instanceof XMPPException)
    {
      StreamError localStreamError = ((XMPPException)paramException).getStreamError();
      if ((localStreamError != null) && ("conflict".equals(localStreamError.getCode())));
    }
    while (true)
    {
      do
        return;
      while (!(isReconnectionAllowed()));
      reconnect();
    }
  }

  protected void notifyAttemptToReconnectIn(int paramInt)
  {
    Iterator localIterator;
    if (isReconnectionAllowed())
      localIterator = this.connection.connectionListeners.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((ConnectionListener)localIterator.next()).reconnectingIn(paramInt);
    }
  }

  protected void notifyReconnectionFailed(Exception paramException)
  {
    Iterator localIterator;
    if (isReconnectionAllowed())
      localIterator = this.connection.connectionListeners.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((ConnectionListener)localIterator.next()).reconnectionFailed(paramException);
    }
  }

  protected void reconnect()
  {
    if (isReconnectionAllowed())
    {
      2 local2 = new Thread(this)
      {
        private int attempts = 0;

        private int timeDelay()
        {
          int i;
          if (this.attempts > 13)
            i = 300;
          while (true)
          {
            while (true)
            {
              return i;
              if (this.attempts <= 7)
                break;
              i = 60;
            }
            i = 10;
          }
        }

        public void run()
        {
          if (!(ReconnectionManager.access$1(this.this$0)))
            return;
          int i = timeDelay();
          if ((!(ReconnectionManager.access$1(this.this$0))) || (i <= 0));
          try
          {
            if (!(ReconnectionManager.access$1(this.this$0)));
            ReconnectionManager.access$2(this.this$0).connect();
          }
          catch (XMPPException localXMPPException)
          {
            while (true)
              this.this$0.notifyReconnectionFailed(localXMPPException);
            try
            {
              Thread.sleep(1000L);
              this.this$0.notifyAttemptToReconnectIn(--i);
            }
            catch (InterruptedException localInterruptedException)
            {
              localInterruptedException.printStackTrace();
              this.this$0.notifyReconnectionFailed(localInterruptedException);
            }
          }
        }
      };
      local2.setName("Smack Reconnection Manager");
      local2.setDaemon(true);
      local2.start();
    }
  }

  public void reconnectingIn(int paramInt)
  {
  }

  public void reconnectionFailed(Exception paramException)
  {
  }

  public void reconnectionSuccessful()
  {
  }
}